﻿using Models;
using System;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    /// <summary>
    /// 修改信息
    /// </summary>
    /// <param name="UserName"></param>
    /// <returns></returns>
    public class UserInfoDAL
    {
        public static bool updateByIDDAL(UserInfo user)
        {
            string sql = string.Format("update tb_UserInfo set UserName='{0}',UserPwd='{1}',QQ='{2}',Phone='{3}' where UserID={4}",user.UserName,user.UserPwd,user.QQ,user.Phone,user.UserID);
            bool res=DBHelper.ExecuteNonQuery(sql);
            return res;
        }
        
        /// <summary>
        /// 根据账号、手机号进行查询
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public static UserInfo loginDAL(string count)
        {
            string sql = string.Format("select * from tb_UserInfo where UserName='{0}'or Phone='{0}'", count);
            SqlDataReader dr = DBHelper.GetDataReader(sql);
            UserInfo user = null;
            if (dr.Read())
            {
                user = new UserInfo();
                user.UserID = Convert.ToInt32(dr["UserID"]);
                user.UserName = dr["UserName"].ToString();
                user.UserPwd = dr["UserPwd"].ToString();
                user.QQ = dr["QQ"] == DBNull.Value ? "" : dr["QQ"].ToString();
                user.Phone = dr["Phone"] == DBNull.Value ? "" : dr["Phone"].ToString();
                user.CreateTime = Convert.ToDateTime(dr["AddTime"]);
                user.State = Convert.ToInt32(dr["State"]);
            }
            //关闭读取器
            dr.Close();
            return user;
        }


        /// <summary>
        /// 获取当前用户状态
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public static string getStateDAL(string count)
        {
            string sql = string.Format("select state from tb_UserInfo where UserName = '{0}'or Phone = '{0}'", count);
            //执行语句
            return DBHelper.ExecuteScalar(sql).ToString();

        }


        /// <summary>
        /// 增加用户
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public static bool addUserDAL(string username,string pwd)
        {
            string sql=string.Format("insert into tb_UserInfo (UserName,UserPwd) values ('{0}','{1}')",username,pwd);
            return DBHelper.ExecuteNonQuery(sql);
        }


        /// <summary>
        /// 根据用户账号名是否重复,true：有记录，false:无记录
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public static bool selectByNameDAL(string username)
        {
           string sql=string.Format("select * from tb_UserInfo where UserName='{0}'",username);
           DataTable dt=DBHelper.GetDataTable(sql);
            if (dt.Rows.Count>0)
            {
                return true;
            }
            return false;

        }
       
    }
}
